package com.hex.ds.hdrs.period.job.service;

import com.hex.ds.hdrs.agent.service.AgentHeartbeatLogHisService;
import com.hex.ds.hdrs.agent.service.AgentHeartbeatLogService;
import com.hex.ds.hdrs.common.constant.Const;
import com.hex.ds.hdrs.common.utils.BaseUtil;
import com.hex.ds.hdrs.common.utils.HdrsDateUtil;
import com.hex.ds.hdrs.cron.service.CronTaskHisService;
import com.hex.ds.hdrs.cron.service.CronTaskService;
import com.hex.ds.hdrs.log.service.*;
import com.hex.ds.hdrs.period.job.po.HisBitch;
import com.hex.ds.hdrs.period.task.service.PeriodTaskHisService;
import com.hex.ds.hdrs.period.task.service.PeriodTaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;


@Slf4j
@Service
public class BackUpDataService {

    @Autowired
    private PeriodTaskHisService periodTaskHisService;

    @Autowired
    private HisBitchService hisBitchService;

    @Autowired
    private PeriodJobHisService periodJobHisService;

    @Autowired
    private PeriodJobService periodJobService;

    @Autowired
    private PeriodTaskService periodTaskService;

    @Autowired
    private CronTaskHisService cronTaskHisService;

    @Autowired
    private CronTaskService cronTaskService;

    @Autowired
    private AgentHeartbeatLogHisService agentHeartbeatLogHisService;

    @Autowired
    private AgentHeartbeatLogService agentHeartbeatLogService;

    @Autowired
    private RunningStateLogHisService runningStateLogHisService;

    @Autowired
    private RunningStateLogService runningStateLogService;

    @Autowired
    private ServerInteractLogHisService serverInteractLogHisService;

    @Autowired
    private ServerInteractLogService serverInteractLogService;

    @Autowired
    private MailSendLogHisService mailSendLogHisService;

    @Autowired
    private MailSendLogService mailSendLogService;

    /**
     * @Method: backUpData <br>
     * @Param: [hisBitch] <br>
     * @Return: void <br>
     * @Description：备份数据<br>
     * @Author： gc.zuo<br>
     * @Date： 2021/6/3 15:56 <br>
     * @Version： V1.0<br>
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void backUpData(HisBitch hisBitch) {
        String bitchType = hisBitch.getBitchType();
        String dateTs = "";
        if (bitchType.equals(Const.CYCLE)) {
            dateTs = hisBitch.getDateTs().replace("-", "");
        } else {
            dateTs = hisBitch.getDateTs() + " 23:59:59";
        }
        Map map = new HashMap(16);
        String saveDate = HdrsDateUtil.getCurrentDate();
        map.put("endDate", dateTs);
        map.put("saveDate", saveDate);
        map.put("bitchType", bitchType);
        if (StringUtils.isNotBlank(hisBitch.getTaskCrtTs())) {
            map.put("taskCrtTs", hisBitch.getTaskCrtTs());
        }
        // 获取saveBatch批次号
        String saveBatch = saveDate + "_" + bitchType + "_" + (this.hisBitchService.qryDayCount(map) + 1);
        map.put("saveBatch", saveBatch);
        hisBitch.setPkId(BaseUtil.getPkId());
        hisBitch.setBitchId(saveBatch);
        hisBitch.setDateTs(saveDate);
        hisBitch.setCrtUser(BaseUtil.getUserId());
        hisBitch.setCrtTs(HdrsDateUtil.getCurTs());
        hisBitch.setUpUser(BaseUtil.getUserId());
        hisBitch.setUpTs(HdrsDateUtil.getCurTs());
        hisBitch.setBitchType(bitchType);
        // 添加备份记录记录
        this.hisBitchService.insert(hisBitch);
        if (bitchType.equals(Const.CYCLE)) {
            // 往历史表添加作业
            periodJobHisService.addJobHis(map);
            // 往历史表添加任务
            this.periodTaskHisService.add(map);
            // 删除周期作业实例任务实例表数据
            periodJobService.delJobByDate(map);
            periodTaskService.delTaskByDate(map);
        } else if (bitchType.equals(Const.CRON)) {
            // 往目标历史表添加数据
            cronTaskHisService.dataBackup(map);
            // 删除源表数据
            cronTaskService.delTaskByDate(map);
        } else if (bitchType.equals(Const.HEARTBEAT_LOG)) {
            // 心跳日志
            // 往目标历史表添加数据
            agentHeartbeatLogHisService.dataBackup(map);
            // 删除源表数据
            agentHeartbeatLogService.delTaskByDate(map);
        } else if (bitchType.equals(Const.RUNNING_LOG)) {
            // 运行状况日志
            // 往目标历史表添加数据
            runningStateLogHisService.dataBackup(map);
            // 删除源表数据
            runningStateLogService.delTaskByDate(map);
        } else if (bitchType.equals(Const.SERVER_LOG)) {
            // 服务器交互日志
            // 往目标历史表添加数据
            serverInteractLogHisService.dataBackup(map);
            // 删除源表数据
            serverInteractLogService.delTaskByDate(map);
        } else if (bitchType.equals(Const.MAIL_SEND_LOG)) {
            // 邮件发送日志
            // 往目标历史表添加数据
            mailSendLogHisService.dataBackup(map);
            // 删除源表数据
            mailSendLogService.delTaskByDate(map);
        }
    }
}
